# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=repmgr
pkgver=5.4.1
pkgrel=2
pkgdesc="Replication Manager for PostgreSQL"
url="https://repmgr.org/"
arch="all"
license="GPL-3.0"
depends="
	cmd:ssh
	doas
	postgresql-client
	"
makedepends="
	curl-dev
	flex
	json-c-dev
	libxml2-dev
	postgresql-dev
	readline-dev
	"
pkgusers="postgres"
pkggroups="postgres"
subpackages="$pkgname-doc $pkgname-daemon"
source="$pkgname-$pkgver.tar.gz::https://github.com/2ndQuadrant/repmgr/archive/v$pkgver.tar.gz
	fix-pg17-compat.patch
	repmgr.conf.patch
	repmgrd.initd
	repmgr.doasd
	"

build() {
	./configure
	make USE_PGXS=1
}

check() {
	./repmgr --version
	./repmgrd --version
}

package() {
	depends="$depends postgresql$(pg_config --major-version)"

	make USE_PGXS=1 DESTDIR="$pkgdir" bindir=/usr/bin install

	install -D -m 640 -o postgres -g postgres \
		repmgr.conf.sample "$pkgdir"/etc/repmgr.conf

	cd "$pkgdir"

	mkdir -p usr/share/doc/$pkgname
	cp "$builddir"/*.md "$builddir"/doc/*.md usr/share/doc/$pkgname/

	install -D -m 600 "$srcdir"/repmgr.doasd etc/doas.d/$pkgname.conf
}

daemon() {
	pkgdesc="A management and monitoring daemon for PostgreSQL Replication Manager"
	depends="$pkgname=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/repmgrd "$subpkgdir"/usr/bin/

	install -D -m 755 "$srcdir"/repmgrd.initd "$subpkgdir"/etc/init.d/repmgrd
}

sha512sums="
5d48c83e68822fa0a8daca9f3ef4c0924caf581d511fa5b0482e64c5a1fd955292d807c9b2a87bb1c4115e9fb17a5733b0f2f49dd24fa26a3b11fb51c1746d12  repmgr-5.4.1.tar.gz
d32cf3e48aa19281defd649076d5cc4a1badd015a1875eda4810ebbd7852fa32704eaadb21dfbe564a29b7b5ffa78354e474c29c6079aad86ba9a82668700a19  fix-pg17-compat.patch
4c4a92a8090ea4674cd1a80f9d17f6c65a672abfe56f2961ac5da1ef73ba80bdfda2cf41f23cab1900e9fff4d6ac099b60d25e120143348b319158f3e9876b7c  repmgr.conf.patch
988d68205290aef2f1f63281d2dc7da7267ba2738d44a8908b0506e968d8acd895c03d9ee821d7ef5d240ec82b67e51fdfb2d9219e71237c1b0aa3f458ad134b  repmgrd.initd
bf2de989323712ee595607a5394bbd0fbadec88d2e46744f6d0b8fce142cf93630868ec7f7108866ca2beb6b29a6999f87fd04fe4d88fa33919f1cd7fc2c76fa  repmgr.doasd
"
